package cn.information.dao;

import cn.information.bean.Product;
import cn.information.bean.Purchase;
import cn.information.utils.DButil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class PurchaseDao {

    public List<Purchase>findAll()
    {
        Connection conn = DButil.getConnection ();
        String sql = "select * from purchase_info";
        Statement stmt = null;
        List<Purchase> list =new ArrayList<>();
        try {
            stmt = conn.createStatement();
            java.sql.ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                Purchase purchase = new Purchase();
                purchase.setSpid(rs.getInt("spid"));
                purchase.setGkid(rs.getInt("gkid"));
                purchase.setGkname(rs.getString("gkname"));
                purchase.setDate(rs.getString("date"));
                purchase.setSpname(rs.getString("spname"));
                purchase.setSpprice(rs.getDouble("spprice"));
                purchase.setSpnumber(rs.getInt("spnumber"));
                purchase.setGkphone(rs.getString("gkphone"));
                list.add(purchase);


            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            DButil.close(conn);
        }
        return list;
    }


    public boolean addPurchase(Purchase purchase) {
        Connection conn = DButil.getConnection ();
        String sql = "insert into purchase_info(spid,gkid,gkname,date,spname,spprice,spnumber,gkphone) values(?,?,?,?,?,?,?,?)";
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,purchase.getSpid());
            pstmt.setInt(2,purchase.getGkid());
            pstmt.setString(3,purchase.getGkname());
            pstmt.setString(4,purchase.getDate());
            pstmt.setString(5,purchase.getSpname());
            pstmt.setDouble(6,purchase.getSpprice());
            pstmt.setInt(7,purchase.getSpnumber());
            pstmt.setString(8,purchase.getGkphone());
            int result = pstmt.executeUpdate();
            if (result > 0) {
                return true;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DButil.close(conn);
        }
        return false;
    }

    public boolean updatePurchase(Purchase purchase) {
        Connection conn = DButil.getConnection();
        String sql = "update purchase_info set gkid=?,gkname=?,date=?,spname=?,spprice=?,spnumber=?,gkphone=? where spid=?";
        PreparedStatement stmt = null;
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, purchase.getGkid());
            stmt.setString(2, purchase.getGkname());
            stmt.setString(3, purchase.getDate());
            stmt.setString(4, purchase.getSpname());
            stmt.setDouble(5, purchase.getSpprice());
            stmt.setInt(6, purchase.getSpnumber());
            stmt.setString(7, purchase.getGkphone());
            stmt.setInt(8, purchase.getSpid());
            if(stmt.executeUpdate()>0){
                return true;
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            DButil.close(conn);
        }
        return false;
    }

    public boolean deletePurchase(int id) {
        Connection conn = DButil.getConnection ();
        String sql = "delete from purchase_info where spid=?";
        PreparedStatement stmt = null;
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, id);
            if(stmt.executeUpdate()>0){
                return true;
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            DButil.close(conn);
        }
        return false;
    }

    public List<Purchase> findByName(String purchaseName) {
        Connection conn = DButil.getConnection();
        String sql = "select * from purchase_info where gkname = ?";
        PreparedStatement stmt = null;
        List<Purchase> list = new ArrayList<>();
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, purchaseName);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                Purchase purchase = new Purchase();
                purchase.setSpid(rs.getInt("spid"));
                purchase.setGkid(rs.getInt("gkid"));
                purchase.setGkname(rs.getString("gkname"));
                purchase.setDate(rs.getString("date"));
                purchase.setSpname(rs.getString("spname"));
                purchase.setSpprice(rs.getDouble("spprice"));
                purchase.setSpnumber(rs.getInt("spnumber"));
                purchase.setGkphone(rs.getString("gkphone"));
                list.add(purchase);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DButil.close(conn);
        }
        return list;
    }
    public List<Purchase> findByDate(String purchaseDate) {
        Connection conn = DButil.getConnection();
        String sql = "select * from purchase_info where date = ?";
        PreparedStatement stmt = null;
        List<Purchase> list = new ArrayList<>();
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, purchaseDate);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                Purchase purchase = new Purchase();
                purchase.setSpid(rs.getInt("spid"));
                purchase.setGkid(rs.getInt("gkid"));
                purchase.setGkname(rs.getString("gkname"));
                purchase.setDate(rs.getString("date"));
                purchase.setSpname(rs.getString("spname"));
                purchase.setSpprice(rs.getDouble("spprice"));
                purchase.setSpnumber(rs.getInt("spnumber"));
                purchase.setGkphone(rs.getString("gkphone"));
                list.add(purchase);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DButil.close(conn);
        }
        return list;
    }
}
